package com.minyifang.wcspf.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.ui.home.model.SpecificationBean;
import com.minyifang.wcspf.work.ui.user.model.DepositBean;
import com.minyifang.wcspf.work.ui.user.presenter.YaJinDetailsPresenter;
import com.minyifang.wcspf.work.ui.user.view.YaJinDetailsView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class YaJinDetailsActivity extends BaseMvpActivity<YaJinDetailsView, YaJinDetailsPresenter> implements YaJinDetailsView {
    private static final String EXTRA_ID = "order_id";

    @BindView(R.id.tv_take_goods_type)
    TextView mTvTakeGoodsType;
    @BindView(R.id.tv_order_sn)
    TextView mTvOrderSn;
    @BindView(R.id.tv_order_status)
    TextView mTvOrderStatus;
    @BindView(R.id.tv_commit_order_time)
    TextView mTvCommitOrderTime;
    @BindView(R.id.tv_pay_type)
    TextView mTvPayType;

    @BindView(R.id.rv_goods)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_order_total_money)
    TextView mTvOrderTotalMoney;
    private String mOrderId;

    private GoodsAdapter mAdapter;
    private List<DepositBean.ComInfoBean> mList;

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, YaJinDetailsActivity.class);
        intent.putExtra(EXTRA_ID, id);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_ya_jin_details;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            mOrderId = intent.getStringExtra(EXTRA_ID);
        }
        mList = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new GoodsAdapter(R.layout.item_yajin, mList);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getDetail(mOrderId);
    }

    @Override
    protected YaJinDetailsPresenter createPresenter() {
        return new YaJinDetailsPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingDialog();
    }

    @Override
    public void showRequestFailed(String message) {
        ToastHelper.getInstance(this).showWarn(message);
    }

    @Override
    public void showDetail(DepositBean bean) {
        mTvTakeGoodsType.setText(bean.getWayMode() == 1 ? "门店自提" : "送货上门");
        mTvOrderSn.setText(bean.getOrderNO());
        mTvOrderStatus.setText(bean.getStatus());
        mTvCommitOrderTime.setText(bean.getOrderTime());
        mTvPayType.setText(bean.getPayMent());
        mTvOrderTotalMoney.setText(String.format("押金总额 ￥%s", bean.getOrderAmount()));

        List<DepositBean.ComInfoBean> comInfo = bean.getComInfo();
        mList.clear();
        mList.addAll(comInfo);
        mAdapter.notifyDataSetChanged();
    }

    class GoodsAdapter extends BaseQuickAdapter<DepositBean.ComInfoBean, BaseViewHolder> {

        public GoodsAdapter(int layoutResId, @Nullable List<DepositBean.ComInfoBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, DepositBean.ComInfoBean item) {
            helper.setText(R.id.item_name, item.getName());
            RecyclerView recyclerView = helper.getView(R.id.item_recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

            recyclerView.setAdapter(new SpecificationAdapter(R.layout.item_yajin_child, item.getSkuList()));
        }
    }

    class SpecificationAdapter extends BaseQuickAdapter<DepositBean.ComInfoBean.SkuListBean, BaseViewHolder> {

        public SpecificationAdapter(int layoutResId, @Nullable List<DepositBean.ComInfoBean.SkuListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, DepositBean.ComInfoBean.SkuListBean item) {
            helper.setText(R.id.item_num, "x" + item.getQuantity());
            helper.setText(R.id.item_depositSum, "￥" + item.getDepositSum() + "元");
            helper.setText(R.id.item_deposit, "单份￥" + item.getDeposit() + "元");
        }
    }
}
